class ServerConfig {
  final String id;
  String name;
  String protocol; // vmess, vless, trojan, shadowsocks, socks
  String address;
  int port;
  String userId;
  String alterId;
  String security;
  String network; // tcp, ws, grpc, etc
  String? path;
  String? host;
  String? tls;
  String? sni;
  String? alpn;
  String? fingerprint;
  int ping;
  DateTime? lastUsed;
  int totalUpload;
  int totalDownload;
  bool isFavorite;
  bool isPinned; // 是否置顶
  String? group;
  String? remarks;
  
  // 位置信息
  String? country;       // 国家名称
  String? countryCode;   // 国家代码（用于国旗）
  String? city;          // 城市

  ServerConfig({
    required this.id,
    required this.name,
    required this.protocol,
    required this.address,
    required this.port,
    this.userId = '',
    this.alterId = '0',
    this.security = 'auto',
    this.network = 'tcp',
    this.path,
    this.host,
    this.tls,
    this.sni,
    this.alpn,
    this.fingerprint,
    this.ping = -1,
    this.lastUsed,
    this.totalUpload = 0,
    this.totalDownload = 0,
    this.isFavorite = false,
    this.isPinned = false,
    this.group,
    this.remarks,
    this.country,
    this.countryCode,
    this.city,
  });

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'protocol': protocol,
      'address': address,
      'port': port,
      'userId': userId,
      'alterId': alterId,
      'security': security,
      'network': network,
      'path': path,
      'host': host,
      'tls': tls,
      'sni': sni,
      'alpn': alpn,
      'fingerprint': fingerprint,
      'ping': ping,
      'lastUsed': lastUsed?.toIso8601String(),
      'totalUpload': totalUpload,
      'totalDownload': totalDownload,
      'isFavorite': isFavorite,
      'isPinned': isPinned,
      'group': group,
      'remarks': remarks,
      'country': country,
      'countryCode': countryCode,
      'city': city,
    };
  }

  factory ServerConfig.fromJson(Map<String, dynamic> json) {
    return ServerConfig(
      id: json['id'],
      name: json['name'],
      protocol: json['protocol'],
      address: json['address'],
      port: json['port'],
      userId: json['userId'] ?? '',
      alterId: json['alterId'] ?? '0',
      security: json['security'] ?? 'auto',
      network: json['network'] ?? 'tcp',
      path: json['path'],
      host: json['host'],
      tls: json['tls'],
      sni: json['sni'],
      alpn: json['alpn'],
      fingerprint: json['fingerprint'],
      ping: json['ping'] ?? -1,
      lastUsed: json['lastUsed'] != null ? DateTime.parse(json['lastUsed']) : null,
      totalUpload: json['totalUpload'] ?? 0,
      totalDownload: json['totalDownload'] ?? 0,
      isFavorite: json['isFavorite'] ?? false,
      isPinned: json['isPinned'] ?? false,
      group: json['group'],
      remarks: json['remarks'],
      country: json['country'],
      countryCode: json['countryCode'],
      city: json['city'],
    );
  }

  ServerConfig copyWith({
    String? id,
    String? name,
    String? protocol,
    String? address,
    int? port,
    String? userId,
    String? alterId,
    String? security,
    String? network,
    String? path,
    String? host,
    String? tls,
    String? sni,
    String? alpn,
    String? fingerprint,
    int? ping,
    DateTime? lastUsed,
    int? totalUpload,
    int? totalDownload,
    bool? isFavorite,
    bool? isPinned,
    String? group,
    String? remarks,
    String? country,
    String? countryCode,
    String? city,
  }) {
    return ServerConfig(
      id: id ?? this.id,
      name: name ?? this.name,
      protocol: protocol ?? this.protocol,
      address: address ?? this.address,
      port: port ?? this.port,
      userId: userId ?? this.userId,
      alterId: alterId ?? this.alterId,
      security: security ?? this.security,
      network: network ?? this.network,
      path: path ?? this.path,
      host: host ?? this.host,
      tls: tls ?? this.tls,
      sni: sni ?? this.sni,
      alpn: alpn ?? this.alpn,
      fingerprint: fingerprint ?? this.fingerprint,
      ping: ping ?? this.ping,
      lastUsed: lastUsed ?? this.lastUsed,
      totalUpload: totalUpload ?? this.totalUpload,
      totalDownload: totalDownload ?? this.totalDownload,
      isFavorite: isFavorite ?? this.isFavorite,
      isPinned: isPinned ?? this.isPinned,
      group: group ?? this.group,
      remarks: remarks ?? this.remarks,
      country: country ?? this.country,
      countryCode: countryCode ?? this.countryCode,
      city: city ?? this.city,
    );
  }

  /// 获取组合唯一标识符
  /// 用于精确匹配节点，避免ID重复导致的高亮错误
  /// 组合：基础ID + 分组 + 节点名称 + 备注
  String getCompositeId() {
    final parts = <String>[id];
    
    if (group != null && group!.isNotEmpty) {
      parts.add('group:$group');
    }
    
    parts.add('name:$name');
    
    if (remarks != null && remarks!.isNotEmpty) {
      parts.add('remarks:$remarks');
    }
    
    return parts.join('|');
  }
  
  /// 检查是否与另一个节点匹配（使用组合ID）
  bool matchesServer(ServerConfig? other) {
    if (other == null) return false;
    return getCompositeId() == other.getCompositeId();
  }
}
